package com.lms.vo;

import com.lms.bean.Role;
import com.lms.bean.User;
import org.springframework.beans.BeanUtils;

/**
 * 视图层对象(View Object)
 * 用于传输到前端页面
 * 为了避免转化成JSON时陷入无穷递归链，所有的的视图层对象(VO)中都不能包含对象字段(例如Book,User)
 */

public class UserVO extends User {
    private Long roleId;
    private String roleName;

    /**
     * 将 Bean 转化成 VO
     */

    public static UserVO convert(User user) {
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        userVO.setRentRecords(null);
        userVO.setRole(null);
        Role role = user.getRole();
        if (role != null) {
            userVO.setRoleId(role.getRoleId());
            userVO.setRoleName(role.getRolename());
        }
        return userVO;
    }

    public Long getRoleId() {
        return roleId;
    }

    public void setRoleId(Long roleId) {
        this.roleId = roleId;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
}
